package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.PermissionService;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 菜单管理
import java.util.*;

/**
 * @ClassName CheckItemController
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/8/20 16:03
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/menu")
public class MenuController {

    // dubbo消费者
    @Reference
    MenuService menuService;
    @Reference
    UserService userService; // 用户


    // 新增菜单（返回 public Result(boolean flag, String message)）
    @RequestMapping(value = "/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            // 成功
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            // 成功
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }

    }

    // 查询菜单（分页）
    @RequestMapping(value = "/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        // 使用查询条件，当前页，当前页最多显示的记录数，查询封装PageResult（总记录数，每页显示的数据List）
        PageResult pageResult = menuService.findPage(queryPageBean.getQueryString(),
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize());
        return pageResult;
    }

    // 根据id，删除菜单
    @RequestMapping(value = "/delete")
    public Result delete(Integer id){
        try {
            menuService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        }catch (RuntimeException e){ // 遇到运行时异常，表示检查项和检查组的关联表中存在数据
            return new Result(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }

    // 使用id查询菜单
    @RequestMapping(value = "/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,menu);
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    // 使用id编辑菜单
    @RequestMapping(value = "/edit")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }

    // 查询所有菜单
    @RequestMapping(value = "/findAll")
    public Result findAll(){
        try {
            List<Menu> list = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,list);
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }









    @RequestMapping(value = "/getMenu")
    public Result getUsername(@RequestBody com.itheima.health.pojo.User user) {
        try {
            // 菜单集合
            LinkedHashSet<Menu> menus = null;
            // 根据当前登录人查询  -1
            com.itheima.health.pojo.User user2 = userService.findByMenuUserUsername(user.getUsername());
            List<com.itheima.health.pojo.User> list = new ArrayList<>();
            list.add(user2);

            // 要返回的数据一级菜单
            Map<String, Object> map = new HashMap<>();
            Set<Role> roles = user2.getRoles();
            for (Role role : roles) {
                menus = role.getMenus();
                map.put("menu", menus);
            }

            // 二级菜单
            for (Menu menu : menus) {
                Integer   id = menu.getId();
                // 返回的数据
                List<Menu> listMenu = menuService.findMenuZii(id);
                menu.setChildren(listMenu);
            }
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }

    }
}
